package cn.edu.dz.mmcock;

public class Kitchen {
    private int food =0;

    public synchronized void do_cock() throws InterruptedException{
        if(food==1){
            this.wait();
        }else {
            System.out.println(Thread.currentThread().getName()+"正在做饭。。。");
            Thread.sleep(2000);
            //做好之后，食物加一
            food++;
            System.out.println("饭做好了。快过来吃吧。。。");
            //全部唤醒
            this.notifyAll();
        }
    }
    public synchronized void do_eat() throws InterruptedException{
        if(food==0){
            this.wait();
        }else {
            //强制性转换
            Child c =(Child) Thread.currentThread();
            //System.out.println(Thread.currentThread().getName()+"抢到了食物，开始吃饭。。。。");
            System.out.println(c.getName()+"抢到了食物，开始吃饭。。。。");

            food--;
            c.setHp(c.getHp()+1);
            System.out.println(c.getName()+"目前的体力值是："+c.getHp());
            System.out.println("food========="+food);
            this.notifyAll();
        }
    }

    public int getFood() {
        return food;
    }

    public void setFood(int food) {
        this.food = food;
    }
}
